package com.sweets.look.ui.widget.supertoast;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sweets.look.R;


public class SuperActivityToast {
    private static final int TOAST_BTN = 4500;
    private static final int TOAST_MSG = 500;
    private boolean isTouchToDismiss;
    private Activity mActivity;
    private TextView mButton;
    private View.OnClickListener mDismissListerner;
    private int mDuration;
    private boolean mIsIndeterminate;
    private boolean mIsTouchDismissible;
    private TextView mMessageTextView;
    private View mToastView;
    private final View.OnTouchListener mTouchDismissListener;
    private ViewGroup mViewGroup;
    private boolean showImmediate;

    public SuperActivityToast(Activity activity, String msg) {
        super();
        this.mIsIndeterminate = false;
        this.mIsTouchDismissible = false;
        this.showImmediate = false;
        this.mDismissListerner = null;
        this.isTouchToDismiss = false;
        this.mTouchDismissListener = new View.OnTouchListener() {
            int timesTouched;

            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(this.timesTouched == 0 && motionEvent.getAction() == 0) {
                    SuperActivityToast.this.dismiss();
                }

                ++this.timesTouched;
                return false;
            }
        };
        if(activity != null) {
            this.mActivity = activity;
            Object v0 = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.mViewGroup = (ViewGroup) activity.findViewById(android.R.id.content);//16908290
            this.mToastView = ((LayoutInflater)v0).inflate(R.layout.widget_toast, this.mViewGroup, false);
            this.mToastView.setFocusable(true);
            this.mToastView.setClickable(true);
            this.mMessageTextView = (TextView) this.mToastView.findViewById(R.id.message_textview);
            this.mButton = (TextView) this.mToastView.findViewById(R.id.button);
            this.mButton.setVisibility(View.GONE);
            this.mDuration = 500;
            this.mMessageTextView.setText(msg);
            this.show();
        }
    }

    public SuperActivityToast(Activity activity, String msg, String btn, final View.OnClickListener listener) {
        super();
        this.mIsIndeterminate = false;
        this.mIsTouchDismissible = false;
        this.showImmediate = false;
        this.mDismissListerner = null;
        this.isTouchToDismiss = false;
        this.mTouchDismissListener = new View.OnTouchListener() {
            int timesTouched;

            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(this.timesTouched == 0 && motionEvent.getAction() == 0) {
                    SuperActivityToast.this.dismiss();
                }

                ++this.timesTouched;
                return false;
            }
        };
        if(activity != null) {
            this.mActivity = activity;
            Object v0 = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.mViewGroup = (ViewGroup) activity.findViewById(android.R.id.content);//16908290
            this.mToastView = ((LayoutInflater)v0).inflate(R.layout.widget_toast, this.mViewGroup, false);
            this.mToastView.setFocusable(true);
            this.mToastView.setClickable(true);
            this.mMessageTextView = (TextView) this.mToastView.findViewById(R.id.message_textview);
            this.mMessageTextView.setText(((CharSequence)msg));
            this.mButton = (TextView) this.mToastView.findViewById(R.id.button);
            this.mButton.setClickable(true);
            this.mButton.setVisibility(View.VISIBLE);
            this.mButton.setText(((CharSequence)btn));
            this.mButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if(listener != null) {
                        listener.onClick(v);
                    }

                    SuperActivityToast.this.dismiss();
                    SuperActivityToast.this.mButton.setClickable(false);
                }
            });
            this.mDuration = 4500;
            this.show();
        }
    }

    public SuperActivityToast(Activity activity, String msg, String btn, final View.OnClickListener listener,
                              View.OnClickListener dismissListener) {
        super();
        this.mIsIndeterminate = false;
        this.mIsTouchDismissible = false;
        this.showImmediate = false;
        this.mDismissListerner = null;
        this.isTouchToDismiss = false;
        this.mTouchDismissListener = new View.OnTouchListener() {
            int timesTouched;
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(this.timesTouched == 0 && motionEvent.getAction() == 0) {
                    SuperActivityToast.this.dismiss();
                }

                ++this.timesTouched;
                return false;
            }
        };
        if(activity != null) {
            this.mActivity = activity;
            LayoutInflater v0 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.mViewGroup = (ViewGroup) activity.findViewById(android.R.id.content);//16908290
            this.mToastView = ((LayoutInflater)v0).inflate(R.layout.widget_toast, this.mViewGroup, false);
            this.mToastView.setFocusable(true);
            this.mToastView.setClickable(true);
            this.mMessageTextView = (TextView) this.mToastView.findViewById(R.id.message_textview);
            this.mMessageTextView.setText(msg);
            this.mButton = (TextView) this.mToastView.findViewById(R.id.button);
            this.mButton.setClickable(true);
            this.mButton.setVisibility(View.VISIBLE);
            this.mButton.setText(btn);
            this.mButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if(listener != null) {
                        listener.onClick(v);
                    }

                    SuperActivityToast.this.dismiss();
                    SuperActivityToast.this.mButton.setClickable(false);
                }
            });
            this.mDuration = 4500;
            this.mDismissListerner = dismissListener;
            this.show();
        }
    }

    public SuperActivityToast(Activity activity) {
        super();
        this.mIsIndeterminate = false;
        this.mIsTouchDismissible = false;
        this.showImmediate = false;
        this.mDismissListerner = null;
        this.isTouchToDismiss = false;
        this.mTouchDismissListener = new View.OnTouchListener() {
            int timesTouched;

            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(this.timesTouched == 0 && motionEvent.getAction() == 0) {
                    SuperActivityToast.this.dismiss();
                }

                ++this.timesTouched;
                return false;
            }
        };
        if(activity != null) {
            this.mActivity = activity;
            LayoutInflater v0 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.mViewGroup = (ViewGroup) activity.findViewById(android.R.id.content);//16908290
            this.mToastView = ((LayoutInflater)v0).inflate(R.layout.widget_toast, this.mViewGroup, false);
            this.mToastView.setFocusable(true);
            this.mToastView.setClickable(true);
            this.mMessageTextView = (TextView) this.mToastView.findViewById(R.id.message_textview);
            this.mButton = (TextView) this.mToastView.findViewById(R.id.button);
            this.mButton.setVisibility(View.GONE);
            this.mDuration = 500;
        }
    }

    public static void cancelAllSuperActivityToasts() {
        ManagerSuperActivityToast.getInstance().cancelAllSuperActivityToasts();
    }

    public static void clearSuperActivityToastsForActivity(Activity activity) {
        ManagerSuperActivityToast.getInstance().cancelAllSuperActivityToastsForActivity(activity);
    }

    public void dismiss() {
        ManagerSuperActivityToast.getInstance().removeSuperToast(this);
    }

    public Activity getActivity() {
        return this.mActivity;
    }

    public View.OnClickListener getDismissListerner() {
        return this.mDismissListerner;
    }

    public int getDuration() {
        return this.mDuration;
    }

    public boolean getShowImmediate() {
        return this.showImmediate;
    }

    public TextView getTextView() {
        return this.mMessageTextView;
    }

    public boolean getTouchToDismiss() {
        return this.isTouchToDismiss;
    }

    public View getView() {
        return this.mToastView;
    }

    public ViewGroup getViewGroup() {
        return this.mViewGroup;
    }

    public boolean isIndeterminate() {
        return this.mIsIndeterminate;
    }

    public boolean isShowing() {
        boolean v0 = this.mToastView == null || !this.mToastView.isShown() ? false : true;
        return v0;
    }

    public boolean isTouchDismissible() {
        return this.mIsTouchDismissible;
    }

    public void setButton(String text, final View.OnClickListener listener) {
        if(this.mButton != null) {
            this.mDuration = 4500;
            this.mButton.setVisibility(View.VISIBLE);
            this.mButton.setText(((CharSequence)text));
            this.mButton.setClickable(true);
            this.mButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if(listener != null) {
                        listener.onClick(v);
                    }

                    SuperActivityToast.this.dismiss();
                    SuperActivityToast.this.mButton.setClickable(false);
                    SuperActivityToast.this.isTouchToDismiss = true;
                }
            });
        }
    }

    public void setDismissListerner(View.OnClickListener listener) {
        this.mDismissListerner = listener;
    }

    public void setDuration(int duration) {
        this.mDuration = duration;
    }

    public void setIndeterminate(boolean isIndeterminate) {
        this.mIsIndeterminate = isIndeterminate;
    }

    public void setShowImmediate(boolean showImmediate) {
        this.showImmediate = showImmediate;
    }

    public void setText(CharSequence text) {
        this.mMessageTextView.setText(text);
    }

    public void setTouchToDismiss(boolean touchDismiss) {
        this.mIsTouchDismissible = touchDismiss;
        if(touchDismiss) {
            this.mToastView.setOnTouchListener(this.mTouchDismissListener);
        }
        else {
            this.mToastView.setOnTouchListener(null);
        }
    }

    public void show() {
        SuperActivityToast.cancelAllSuperActivityToasts();
        ManagerSuperActivityToast.getInstance().add(this);
    }
}

